import {unioformDateTransform} from '../../utils/index.js'
import {handleSql} from '../../database/mysql/index.js'


class recentPlansModel {
    constructor() {
        this.tableName = 'recent_plans';
    }
    
    // 根据用户id和角色id获取近期计划
    async getRecentPlansByUserIdAndRoleCode({userId,roleCode}) {
        const sql = `SELECT * FROM ${this.tableName} 
                     WHERE userId = ? AND roleCode = ?`
        let result = await handleSql(sql,[userId,roleCode])
        return result
    }



    // 新增近期计划  —— 新增单条
    async addRecentPlan(recentPlanData) {
        const {userId, roleCode, planName,deadline } = recentPlanData;
        const createTime = unioformDateTransform(new Date())
        const sql = `INSERT INTO ${this.tableName}(userId,roleCode,planName,deadline,createTime) 
                     VALUES (?,?,?,?,?) `
        let result = await handleSql(sql,[userId,roleCode,planName,deadline,createTime])
        if(result.affectedRows == 1){
            return true
        }else{
            return false
        }
    }



    // 新增近期计划 —— 批量新增  __待完善
    async addBatchRecentPlan({userInfo,recentPlanDataList}) {
        const {userId, roleCode} = userInfo;
        let result = await recentPlanDataList.map( async planData => {
            return await this.addRecentPlan({...planData,userId,roleCode})
        })
        return Promise.all(result).then(res=>{
            return true
        }).catch(err=>{
            return false
        })
    }

}   

export default new recentPlansModel();